<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
      <!-- table -->
      <BaseDialog :visible.sync="suppliers" :title="'供应商选择'" width="850px" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
        <SupplierList @close="suppliersClose" @save="suppliersSave" :rowDate="rowDate"></SupplierList>
      </BaseDialog>
      <FormPanel ref="formPanel" :list="formList1" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
      <div style="padding-top: 1rem;"></div>
      <div
        :style="{
          position: 'absolute',
          left: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'right'
        }"
      >
        <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
      <BaseDialog :visible.sync="visible" :title="'备件选择'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
        <Vehicle @close="close" @save="partSave"></Vehicle>
      </BaseDialog>
    </Spin>
  </div>
</template>
<script>
import { getAppId} from '@/assets/js/auth';
import {dictionary} from'@/utils/dictMixin';
import Vehicle from '@/pages/claimDemage/tools/vehicle.vue';
import { getSupplierList } from '@/api/dictionary/index';
import { newSave } from '@/api/claim/factory/PartWarrantyType';
import { getModel } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';
import SupplierList from '@/pages/claimDemage/tools/SuppliersList.vue';
export default {
  mixins: [dictionary],
  components: {
    Vehicle,
    SupplierList,
  },
  data() {
    return {
      loading: false,
      dataList1: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      rowDate: {},
      suppliers: false,
      infoId: null,
      flterValue: {}, //参数
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList1: this.createFormList1(),
      saveData: null,
      activeName: '审核记录'
    };
  },
  created() {
    //this.chuqi();
    this.getSeriesStr();
  },
  methods: {
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.loading = true;
        this.saveData.supId = this.supId;
        this.saveData.supplierCode = this.supCode;
        let res = await newSave(this.saveData);
        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({title: '提示信息', message: '保存成功'});
        } else {
          this.loading = false;
          // this.$notify.error({title: '提示信息', message: res.errMsg || '保存失败!'});
          // this.closeDialog();
        }
      } else {
        // return this.$notify.warning({title: '提示信息', message: '有未校验通过的信息'});
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', {visible: false, data: this.selectedRows, type: 'close'});
    },
    add() {
    },
    async chuqi() {

    },
    selectSuppliersCode() {
      this.suppliers = true;
    },
    suppliersClose(){
      this.suppliers = false;
    },
    suppliersSave(obj) {
      this.formList1.find(x => x.fieldName == 'supplierCode').initialValue = obj[0].supCode;
      this.formList1.find(x => x.fieldName == 'supplierName').initialValue = obj[0].supName;
      this.supCode = obj[0].supCode;
      this.supId = obj[0].supId;
      this.suppliers = false;
    },
    chooseOrderNo() {
      this.visible = true;
    },
    async partSave(obj) {
      console.log(obj);
      this.formList1.find(x => x.fieldName == 'partCode').initialValue = obj[0].partCode;
      this.formList1.find(x => x.fieldName == 'partName').initialValue = obj[0].partName;
      // this.formList1.find(x => x.fieldName == 'partUnit').initialValue = obj[0].partUnit;
      this.rowDate.partId = obj[0].partId;
      this.rowDate.partCode = obj[0].partCode;
      this.formList1.find(x => x.fieldName == 'supplierCode').initialValue = '';
      this.formList1.find(x => x.fieldName == 'supplierName').initialValue = '';

      this.rowDate.partId = obj[0].partId;

      const res  = await getSupplierList({appId : getAppId(),id : obj[0].partId});

      if (res.resultCode == 200) {
        res.data.records.map(x => {
          x.supCode = x.bjgybm;
          x.supId = x.supplierId;
          x.supName = x.bjgyjc;
        })
        if(res.data.records.length==1){
          this.suppliersSave(res.data.records)
        }else if(res.data.records.length==0){
          this.rowDate.partCode=""
        }
      }
      this.visible = false;
    },
    close() {
      this.visible = false;
    },
    //适用车型下拉
    async getSeriesStr() {

      const res1 = await getModel();
      if (res1.resultCode == 200) {

        this.formList1.find(x => x.fieldName == 'series').itemList = res1.data.map(y => ({ text: y.MODEL_NAME, value: y.MODEL_CODE }));
      }
    },
    createFormList1() {
      return [
        {
          type: 'INPUT',
          label: '备件代码',
          fieldName: 'partCode',
          readonly: true,// 只读
          placeholder: '请选择备件代码...',
          rules: [{ required: true, message: '请选择备件代码...', trigger: 'blur' }],
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '备件名称',
          readonly: true,// 只读
          disabled: true,// 置会
          fieldName: 'partName',
          placeholder: '请输入备件名称...',
          offsetRightCols: 1
        },
        { type: 'INPUT',
          label: '供应商代码',
          fieldName: 'supplierCode',
          placeholder: '请选择供应商...',
          readonly: true,
          rules: [{ required: true, message: '请输入供应商', trigger: 'blur' }],
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.selectSuppliersCode()} />
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '供应商名称',
          readonly: true,// 只读
          disabled: true,// 置会
          fieldName: 'supplierName',
          placeholder: '请输入供应商名称...',
          offsetRightCols: 1
        },
        {
          type: 'SELECT',
          label: '备件类型',
          fieldName: 'warrantyType',
          placeholder: '请选择',
          initialValue: '',
          rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          filterable: true,
          itemList: this.createDictList('5509')
        },
        {
          type: 'SELECT',
          label: '返件类型',
          fieldName: 'returnType',
          placeholder: '请选择',
          initialValue: '',
          rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          filterable: true,
          itemList: this.createDictList('5585',[55851002])
        },
        {
          type: 'SELECT',
          label: '是否特批',
          placeholder: '请选择...',
          fieldName: 'isSpecially',
          rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '是否辅料',
          placeholder: '请选择...',
          fieldName: 'isAccessories',
          rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '是否附件',
          placeholder: '请选择...',
          fieldName: 'isAttached',
          rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '适用车型',
          placeholder: '请选择...',
          fieldName: 'series',
          rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          itemList:[]
        },
        {
          type: 'SELECT',
          label: '状态',
          placeholder: '请选择...',
          fieldName: 'ptStatus',
          initialValue: 10041001,
          rules: [{ required: true, message: '请选择...', trigger: 'blur' }],
          itemList: [
            { text: '启用', value: 10041001 },
            { text: '停用', value: 10041002 }
          ],
        },
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
